/**
 * Authors:
 * Vladimír Matěna
 * Jan Skalický
 */
package inireader;

import java.util.regex.Pattern;

/**
 * Class keeping identifier of section or property.
 */
public final class Identifier
{
	private String id;

	/**
	 * Creates identifier. Check if it is correctly formatted.
	 * @param id identifier string
	 */
	public Identifier(String id)
	{
		if (isCorrectlyFormated(id))
		{
			this.id = id;
		}
		else
		{
			throw new IncorrectFormatException(String.format("%s doesn't match correct format for Identifier.id.", id));
		}
	}

	/**
	 * Gets id.
	 * @return string id
	 */
	public String getId()
	{
		return id;
	}

	/**
	 * Check if id is correctly formatted.
	 * @param id id to check
	 * @return true if it is correctly formatted, otherwise false
	 */
	private boolean isCorrectlyFormated(String id)
	{
		return Pattern.matches("[A-Za-z.$:][a-zA-Z0-9~.:$_ -]*", id);
	}

	/**
	 * Compares two identifier for equality.
	 * @param identifier identifier to compare with
	 * @return true of equal, false otherwise
	 */
	public boolean equals(Identifier identifier)
	{
		return id.equals(identifier.id);
	}

	/**
	 * String id of Identifier.
	 * @return id
	 */
	@Override
	public String toString()
	{
		return id;
	}
}//end Identifier

